package com.wave12.utility;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

public class EmailUtil {
    private static final Logger logger = LoggerFactory.getLogger(EmailUtil.class);

    private String mSmtpSever = "";
    private String mAccessCode = "";
    private String mFromEmail = "";
    public void setEmailServer(String smtpServer, String fromEmail, String accessCode){
        mSmtpSever = smtpServer;
        mAccessCode = accessCode;
        mFromEmail = fromEmail;
    }
    public void sendEmail(String toEmail, String title, String body) throws MessagingException {
        Properties props = new Properties();
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.transport.protocol", "smtp");
        props.put("mail.smtp.host",mSmtpSever);// smtp服务器地址

        Session session = Session.getInstance(props);
        session.setDebug(true);

        Message msg = new MimeMessage(session);
        msg.setSubject(title);
        // msg.setText(body);   // 纯文本
        msg.setContent(body, "text/html;charset=utf-8");
        msg.setFrom(new InternetAddress(mFromEmail));//发件人邮箱(我的163邮箱)
        msg.setRecipient(Message.RecipientType.TO,
                new InternetAddress(toEmail)); //收件人邮箱(我的QQ邮箱)
        msg.saveChanges();

        Transport transport = session.getTransport();
        transport.connect(mFromEmail,mAccessCode);//发件人邮箱,授权码(可以在邮箱设置中获取到授权码的信息)

        transport.sendMessage(msg, msg.getAllRecipients());

        System.out.println("邮件发送成功...");
        logger.info("邮件发送成功,目标：" + toEmail);
        transport.close();
    }

    public void sendEmailWithAttach(String toEmail, String title, String body, String[] attachs) throws MessagingException, UnsupportedEncodingException {
        Properties props = new Properties();
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.transport.protocol", "smtp");
        props.put("mail.smtp.host",mSmtpSever);// smtp服务器地址

        Session session = Session.getInstance(props);
        session.setDebug(true);

        Message msg = new MimeMessage(session);
        msg.setSubject(title);
        // msg.setText(body);   // 纯文本
        // msg.setContent(body, "text/html;charset=utf-8");
        msg.setFrom(new InternetAddress(mFromEmail));    //发件人邮箱
        msg.setRecipient(Message.RecipientType.TO,
                new InternetAddress(toEmail));           //收件人邮箱
        // 正文
        Multipart mp = new MimeMultipart();
        MimeBodyPart mbp = new MimeBodyPart();
        mbp.setContent(body, "text/html;charset=utf-8");
        mp.addBodyPart(mbp);

        // 附件
        for (int i = 0; i < attachs.length; ++i) {
            mbp = new MimeBodyPart();
            FileDataSource fds = new FileDataSource(attachs[i]); //得到数据源
            mbp.setDataHandler(new DataHandler(fds)); //得到附件本身并至入BodyPart
            mbp.setFileName(MimeUtility.encodeText(fds.getName()));  //得到文件名同样至入BodyPart
            mp.addBodyPart(mbp);
        }
        msg.setContent(mp); //Multipart加入到信件
        msg.saveChanges();

        Transport transport = session.getTransport();
        transport.connect(mFromEmail,mAccessCode);//发件人邮箱,授权码(可以在邮箱设置中获取到授权码的信息)

        transport.sendMessage(msg, msg.getAllRecipients());

        logger.info("带附件邮件发送成功,目标：" + toEmail);
        transport.close();
    }
}
